/*********************************************************************
*
* Software License Agreement (BSD License)
*
*  Copyright (c) 2010, Heriot-Watt University, UK.
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Heriot-Watt University nor the names of
*     its contributors may be used to endorse or promote products
*     derived from this software without specific prior written
*     permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*
*  Author: Joel Cartwright
*
*********************************************************************/

#include <matchmaker/service_advertiser.h>

namespace matchmaker
{

// ------------------------------------------------------------
// Public methods
// ------------------------------------------------------------

ServiceAdvertiser::ServiceAdvertiser()
{

}


ServiceAdvertiser::ServiceAdvertiser(const ServiceAdvertiser& rhs)
: impl_(rhs.impl_)
{

}


ServiceAdvertiser::~ServiceAdvertiser()
{

}


void ServiceAdvertiser::shutdown()
{
  impl_.reset();
}


std::string ServiceAdvertiser::getName() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceAdvertiser::getSoaName called on null monitor.");
    return std::string();
  }

  return impl_->advertMsg_.service_name;
}


void ServiceAdvertiser::setStatus(ServiceStatus::Enum value, const std::string extraStatus)
{
  setStatus(ServiceStatus(value, extraStatus));
}


void ServiceAdvertiser::setStatus(const ServiceStatus& status)
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceAdvertiser::setStatus called on null monitor.");
    return;
  }

  ros::Time oldStamp = impl_->advertMsg_.status_stamp;

  // Set status timestamp. Message timestamp is set by client library before publishing.
  impl_->advertMsg_.status_stamp = ros::Time::now();

  if (oldStamp.isZero() || !status.hasValue(impl_->advertMsg_.status)
      || !status.hasExtraStatus(impl_->advertMsg_.extra_status))
  {
    ROS_INFO_STREAM("ServiceAdvertiser::setStatus: Sending modified advert.");
    // If first update or status changed, send the advert straight away.
    // Otherwise sending is performed periodically by matchmaker::Client class.
    impl_->advertMsg_.status = status.getValue();
    impl_->advertMsg_.extra_status = status.getExtraStatus();
    impl_->advertiseCallback_(this);
  }
}


ServiceStatus ServiceAdvertiser::getStatus() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceAdvertiser::getStatus called on null monitor.");
    return ServiceStatus::REMOVED;
  }

  return ServiceStatus((ServiceStatus::Enum)impl_->advertMsg_.status, impl_->advertMsg_.extra_status);
}


ros::Time ServiceAdvertiser::getStatusTime() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceAdvertiser::getLastStatusTime called on null monitor.");
    return ros::Time();
  }

  return impl_->advertMsg_.status_stamp;
}


// ------------------------------------------------------------
// Private methods
// ------------------------------------------------------------

ServiceAdvertiser::Impl::Impl()
: creationTime_(ros::Time::now())
{

}


ServiceAdvertiser::Impl::~Impl()
{

}


ServiceAdvertiser::ServiceAdvertiser(const std::string& soaName,
    const std::string & topic,
    boost::function<void (ServiceAdvertiser *)> advertiseCallback)
: impl_(new Impl())
{
  impl_->advertiseCallback_ = advertiseCallback;

  // Leave status_change_stamp unset.
  impl_->advertMsg_.status = matchmaker_msgs::Advert::STATUS_REMOVED;
  impl_->advertMsg_.topic = topic;
  impl_->advertMsg_.service_name = soaName;
}


matchmaker_msgs::Advert* ServiceAdvertiser::getAdvertMsg()
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceAdvertiser::getAdvertMsg called on null monitor.");
    return NULL;
  }

  return &(impl_->advertMsg_);
}


ros::Time ServiceAdvertiser::getCreationTime() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceAdvertiser::getCreationTime called on null monitor.");
    return ros::Time();
  }

  return impl_->creationTime_;
}


long int ServiceAdvertiser::getNumCopies()
{
   if (!impl_) return 0;

   return impl_.use_count();
}


}
